package com.qys.livesMall.finance.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qys.livesMall.common.constants.Constants;
import com.qys.livesMall.finance.dao.FinanceShopAccountDao;
import com.qys.livesMall.finance.mapper.FinanceShopAccountMapper;
import com.qys.livesMall.finance.model.entity.shop.FinanceShopAccount;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 店铺余额表 服务实现类
 * </p>
 *
 * @author
 * @since 2023-06-06
 */
@Service
public class FinanceShopAccountDaoImpl extends ServiceImpl<FinanceShopAccountMapper, FinanceShopAccount> implements FinanceShopAccountDao {

    @Override
    public FinanceShopAccount getByUserId(Long userId) {
        LambdaQueryWrapper<FinanceShopAccount> qw=new LambdaQueryWrapper<>();
        qw.eq(FinanceShopAccount::getUserId,userId);
        qw.eq(FinanceShopAccount::getDeleted, Constants.YesNo.NO);
        qw.last("limit 1");
        return getOne(qw);
    }

    @Override
    public List<FinanceShopAccount> getByUserIds(List<Long> userIds) {
        LambdaQueryWrapper<FinanceShopAccount> qw=new LambdaQueryWrapper<>();
        qw.in(FinanceShopAccount::getUserId,userIds);
        qw.eq(FinanceShopAccount::getDeleted, Constants.YesNo.NO);
        return list(qw);
    }

    @Override
    public FinanceShopAccount getByShopId(Long shopId) {
        LambdaQueryWrapper<FinanceShopAccount> qw=new LambdaQueryWrapper<>();
        qw.eq(FinanceShopAccount::getShopId,shopId);
        qw.eq(FinanceShopAccount::getDeleted, Constants.YesNo.NO);
        qw.last("limit 1");
        return getOne(qw);
    }

    @Override
    public List<FinanceShopAccount> liatByShopIds(List<Long> shopIds) {
        LambdaQueryWrapper<FinanceShopAccount> qw=new LambdaQueryWrapper<>();
        qw.in(FinanceShopAccount::getShopId,shopIds);
        qw.eq(FinanceShopAccount::getDeleted, Constants.YesNo.NO);
        return list(qw);
    }

    @Override
    public BigDecimal getTotalBalance(Integer userSysType) {
        return baseMapper.getTotalBalance(userSysType);
    }

    @Override
    public List<Long> listByUserIds(List<Long> userIds, BigDecimal minBalance, BigDecimal maxBalance) {
        LambdaQueryWrapper<FinanceShopAccount> qw=new LambdaQueryWrapper<>();
        qw.select(FinanceShopAccount::getUserId);
        boolean exc=false;
        if (userIds!=null){
            if (userIds.size()==0){
                userIds.add(-1L);
            }
            qw.in(FinanceShopAccount::getUserId, userIds);
            exc=true;
        }

        if (minBalance!=null) {
            qw.ge(FinanceShopAccount::getTotalBalance, minBalance);
            exc=true;
        }
        if (maxBalance!=null) {
            qw.le(FinanceShopAccount::getTotalBalance, maxBalance);
            exc=true;
        }
        if (!exc){
            return null;
        }
        qw.eq(FinanceShopAccount::getDeleted, Constants.YesNo.NO);
        List<FinanceShopAccount> list= list(qw);
        if (CollectionUtils.isNotEmpty(list)){
            return list.stream().map(FinanceShopAccount::getUserId).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

    @Override
    public List<Long> listShopIdByLessBalance(List<Long> shopIds, BigDecimal balance) {
        LambdaQueryWrapper<FinanceShopAccount> qw=new LambdaQueryWrapper<>();
        qw.in(FinanceShopAccount::getShopId,shopIds);
        qw.lt(FinanceShopAccount::getAvailableBalance, balance);
        List<FinanceShopAccount> accounts= list(qw);
        return accounts!=null?accounts.stream().map(FinanceShopAccount::getShopId).collect(Collectors.toList()) : null;
    }


}
